package com.homwee.factory.tvtool;

import com.homwee.factory.R;

import android.app.Service;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.homwee.factory.tvtool.SimpleAlertDialog;

public class ReproductionLayout extends LinearLayout {
	private Context mContext;
	private WindowManager mWindowManager;
	private Service mService;
	private Button saveButton;
	private SimpleAlertDialog simpleAlertDialog;
	private long mExitTime; 
	
	public ReproductionLayout(Context context, Service sv) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		mService = sv;
		mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		LayoutInflater.from(mContext).inflate(
				R.layout.reproduction_rate_service, this);
		saveButton = (Button) findViewById(R.id.bt_save);

		View.OnClickListener exitListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				simpleAlertDialog.hide();
				mWindowManager.removeView(ReproductionLayout.this);
				mService.stopSelf();				
			}
		};

		
		simpleAlertDialog = new SimpleAlertDialog(mContext,exitListener);
		
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			if((System.currentTimeMillis() - mExitTime) > 1000)
			{
//				Toast.makeText(getApplicationContext(), "鍐嶆寜涓�娆￠��鍑虹▼搴�", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			}
			else
			{	
//				StaticValues.bSaveAQAndExit = true;
				simpleAlertDialog.show();				
			}
			return true;
		default:
			return super.dispatchKeyEvent(event);
		}

	}

}
